package com.neu.flashmall.ms.service.impl;

import com.neu.flashmall.ms.entity.SysRole;
import com.neu.flashmall.ms.mapper.SysRoleMapper;
import com.neu.flashmall.ms.mapper.SysUserMapper;
import com.neu.flashmall.ms.service.SysMenuService;
import com.neu.flashmall.ms.service.SysRoleMenuService;
import com.neu.flashmall.ms.service.SysRoleService;
import com.neu.flashmall.ms.vo.RoleVo;
import com.neu.flashmall.ms.vo.SysMenuItemVo;
import com.neu.flashmall.ms.vo.SysMenuVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     *
     * @return RoleVo对象中，填充了SysMenuVo属性，且给SysMenuItemVo中的has属性赋值了
     * @throws CloneNotSupportedException
     */
    @Override
    public List<RoleVo> getAllRole() throws CloneNotSupportedException {
        List<SysRole> allRole = this.sysRoleMapper.selectAll();
        SysMenuVo source = this.sysMenuService.createSysMenuVoLevel();
        List<RoleVo> result = new ArrayList<>();
        for(SysRole role : allRole){
            Integer id = Integer.valueOf(String.valueOf(role.getId()));
            List<Long> roleMenuIdList = this.sysRoleMenuService.getMenuIdByRole(id);
            Set<Long> set = new HashSet<>(roleMenuIdList);
            RoleVo roleVo = new RoleVo();
            SysMenuVo roleMenu = (SysMenuVo) source.clone();
            this.toChooseMenuOfRole(roleMenu.getData(),set);

            roleVo.setSysMenuVo(roleMenu);
            roleVo.setSysRole(role);
            roleVo.setAuthority(roleMenuIdList);

            result.add(roleVo);
        }

        return result;
    }

    /**
     *
     * @return RoleVo对象中，没填充SysMenuVo属性
     * @throws CloneNotSupportedException
     */
    @Override
    public List<RoleVo> getAllRoleWithoutSysMenuVo() throws CloneNotSupportedException {
        List<SysRole> allRole = this.sysRoleMapper.selectAll();
        List<RoleVo> result = new ArrayList<>();
        for(SysRole role : allRole){
            Integer id = Integer.valueOf(String.valueOf(role.getId()));
            List<Long> roleMenuIdList = this.sysRoleMenuService.getMenuIdByRole(id);
            RoleVo roleVo = new RoleVo();
            roleVo.setSysRole(role);
            roleVo.setAuthority(roleMenuIdList);
            result.add(roleVo);
        }

        return result;
    }

    @Override
    public String deleteRoleInfo(Long id) {
        if(id.equals(1)){
            return "超级管理员无法删除";
        }
        if(id.equals(2)){
            return "默认用户无法删除";
        }
        int deleteRole = this.sysRoleMapper.deleteByPrimaryKey(id);
        int deleteRoleMenu = this.sysRoleMenuService.deleteRoleMenuByRole(id);
        Integer roleId = Integer.valueOf(String.valueOf(id));
        int setToDefault = this.sysUserMapper.setOneRoleUserToDefault(roleId);
        if(deleteRole > 0){
            return "删除用户类型成功";
        }
        return  "数据库异常";
    }

    @Override
    public String addRole(SysRole role) {
        if(role.getName().contains(" ")){
            return "用户类型名称不能带有空格";
        }
        SysRole sysRole = this.sysRoleMapper.selectByName(role.getName());
        if(sysRole != null){
            return "该用户类型已经存在";
        }
        int result = this.sysRoleMapper.insertSelective(role);
        String roleName = role.getName();
        role = null;
        role = this.sysRoleMapper.selectByName(roleName);
        if(result > 0 && role != null){
            return "添加用户类型成功";
        }
        return "数据库异常";
    }

    @Override
    public List<SysRole> getAllRoleOnly() {
        return this.sysRoleMapper.selectAll();
    }


    private void toChooseMenuOfRole(List<SysMenuItemVo> sysMenuVoList,Set<Long> set){
        for(SysMenuItemVo item1 : sysMenuVoList){
            if(set.contains(item1.getId())){
                item1.setHas(true);
            }
            List<SysMenuItemVo> children = item1.getChildren();
            if(children != null){
                toChooseMenuOfRole(children,set);
            }
        }
    }
}
